package swing.dialog;

import api.MainServer;
import client.UserSession;
import com.syuchen.protocol.http.Response;
import swing.FrameManager;
import swing.customComp.CustomStyles;
import swing.customComp.HintJTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 注册对话框
 */
public class AddGroupDialog extends JDialog {

    public JPanel registerPanel;

    private JLabel groupStaticText;
    private HintJTextField groupField;
    private JButton registerButton;

    private ActionListener registerActionListener;

    public AddGroupDialog(JFrame parentFrame) {
        this.setTitle("添加群组");
        this.setSize(400,300);
        this.setResizable(false);
        this.setLocationRelativeTo(parentFrame);
        registerPanel = new JPanel();
        registerPanel.setLayout(null);
        initAllComponent();
        addAllComponent();
        SwingUtilities.invokeLater(() -> registerButton.requestFocusInWindow());
        this.setContentPane(registerPanel);
    }

    private void initAllComponent() {
        groupStaticText = new JLabel("输入群组名字：");
        Font staticTextFont = new Font("宋体", Font.BOLD,16);
        groupStaticText.setFont(staticTextFont);
        groupStaticText.setBounds(30,20,200,20);
        //账户输入框
        groupField = new HintJTextField();
        groupField.setBounds(30, 45, 285, 35);
        groupField.setFont(CustomStyles.inputFont);
        groupField.setHintText("请输入4-24位字符的群组名字");


        //注册按钮
        registerButton = new JButton("添加");
        registerButton.setBounds(30, 150, 285, 35);
        registerActionListener = listen -> {
            String group_name = groupField.getText().trim();
            new Thread(() -> {
                addGroup(group_name);
            }).start();
        };
        registerButton.addActionListener(registerActionListener);
    }

    private void addAllComponent() {
        registerPanel.add(groupStaticText);
        registerPanel.add(groupField);
        registerPanel.add(registerButton);
    }

    private void enterAddGroupState() {
        SwingUtilities.invokeLater(() -> {
            registerButton.setText("添加中...");
            registerButton.removeActionListener(registerActionListener);
            groupField.setEnabled(false);
        });
    }

    private void exitAddGroupState() {
        SwingUtilities.invokeLater(() -> {
            registerButton.setText("添加");
            registerButton.addActionListener(registerActionListener);
            groupField.setEnabled(true);
        });
    }

    public void addGroup(String group_name) {
        if (group_name.isEmpty()) {
            showMessageDialog("输入不能为空！");
            return;
        }
        if (group_name.length() < 4 || group_name.length() > 24) {
            showMessageDialog("名字长度不合法！");
            return;
        }
        try {
            this.enterAddGroupState();
            Response response = MainServer.groupAction.add_group(UserSession.user.getId(), group_name);
            if (response.getStatus_code() != Response.OK) {
                showMessageDialog(response.getStatus_code() + " " + response.getStatus_description());
                return;
            }
            FrameManager.mainFrame.updateGroupList();
            showMessageDialog("添加成功");
            this.dispose();
        } catch (Exception e) {
            showMessageDialog("服务器连接失败！");
            e.printStackTrace();
        }  finally {
            SwingUtilities.invokeLater(this::exitAddGroupState);
        }
    }

    private void showMessageDialog(String message) {
        JOptionPane.showMessageDialog(registerPanel, message);
    }

    private int showConfirmDialog(String message) {
        return JOptionPane.showConfirmDialog(registerPanel, message, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
    }
}
